<template>
  <div>
    <top-list :activeIndex="1+''"></top-list>
<!--    <el-menu-->
<!--      :default-active="activeIndex"-->
<!--      class="el-menu-demo"-->
<!--      mode="horizontal"-->
<!--      @select="handleSelect"-->
<!--      background-color="#545c64"-->
<!--      text-color="#fff"-->
<!--      active-text-color="#ffd04b">-->
<!--      <el-menu-item index="0" style="margin-right: 15%">文件列表</el-menu-item>-->
<!--      <el-menu-item index="1" style="margin-right: 15%">数据监控</el-menu-item>-->
<!--      <el-menu-item index="2" style="margin-right: 15%">任务信息</el-menu-item>-->
<!--      <el-menu-item index="3" style="margin-right: 15%">受试者列表</el-menu-item>-->
<!--      <el-menu-item index="4" >设备管理</el-menu-item>-->
<!--    </el-menu>-->
    <el-button plain type="primary" @click="deviceEdit(0)" :disabled="!isSiteRole" style="margin-left: 20px;margin-top: 20px">添加设备</el-button>
    <el-table
      :data="dataList"
      border
      stripe
      v-loading="dataListLoading"
      style="width: 100%; margin-top: 20px; margin-left: 20px">
      <el-table-column prop="equipmentType" header-align="center" align="center" label="设备型号"></el-table-column>
<!--      <el-table-column prop="refer_name" header-align="center" align="center" label="参考导联名"></el-table-column>-->
      <el-table-column prop="leadNumber" header-align="center" align="center" label="脑电导联数"></el-table-column>
<!--      <el-table-column prop="electrode_distribution" header-align="center" align="center" label="电极坐标分布"></el-table-column>-->
      <el-table-column prop="eyeElectrodes" header-align="center" align="center" label="眼电电极"></el-table-column>
      <el-table-column prop="electrocardioElectrode" header-align="center" align="center" label="心电电极"></el-table-column>
      <el-table-column prop="electrodeMaterial" header-align="center" align="center" label="电极材料"></el-table-column>
      <el-table-column prop="description" header-align="center" align="center" label="描述"></el-table-column>
      <el-table-column prop="siteName" header-align="center" align="center" label="中心名称"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="250" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="deviceEdit(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="deviceDelete(scope.row.id,scope.row.equipmentType)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px;"
    >
    </el-pagination>
    <device-update v-if="updateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></device-update>
  </div>
</template>

<script>
import deviceUpdate from './device-update'
import device from '../../api/device'
import myUtils from '../../api/myUtils'
import TopList from '@/components/TopList'
export default {
  components: { deviceUpdate,TopList },
  data() {
    return {
      activeIndex: '4',
      updateVisible:false,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      isSiteRole:false
    }
  },
  created() {
    this.getDataList();
    let role =myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
    if(role == 4 || role == 5 || role == 6 || role == 7 || role == 8)
    {
      this.isSiteRole = true;
    }
  },
  methods: {
    handleSelect(key, keyPath) {
      if(key==='0')
      {
        this.$router.push({path:`/upload/file-list?subjectPkId=${this.$route.query.subjectPkId}`})
      }
      if(key==='1')
      {
        // this.$router.push(0)
        this.$router.push({path:`/upload/research-manager?subjectPkId=${this.$route.query.subjectPkId}`})
      }
      else if(key==='2')
      {
        this.$router.push({path:`/upload/task?subjectPkId=${this.$route.query.subjectPkId}`})
      }
      else if(key==='3')
      {
        this.$router.push({path:`/upload/subject-manager?subjectPkId=${this.$route.query.subjectPkId}`})
      }
      else if(key==='4')
      {
        this.$router.push({path:`/upload/device?subjectPkId=${this.$route.query.subjectPkId}`})
      }
    },

    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },

    getDataList()
    {
      let role = myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator)
        if(role == 2 || role == 3 || role == 6 || role == 7 || role == 8)
        {
          device.getEquipmentByUser(this.$store.getters.name,this.pageSize,this.pageIndex).then(
            (response)=>{
              this.dataList = response.data.records;
              this.totalPage = response.data.total;
            })
        }
        else{
          device.equipmentSearch(myUtils.getSite(this.$route.query.subjectPkId,this.$store.getters.roles),this.pageIndex,this.pageSize).then(
            (response)=>{
              this.dataList = response.data.records;
              this.totalPage = response.data.total;
            })

        }
    },

    deviceEdit(id)
    {
      let role =myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
      if(role !== 4 && role !== 5 && role !== 6 && role !== 7) //采集和队列管理员无权
      {
        this.$message.error("您不是分中心管理员");
        return;
      }
      this.updateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
      //@refreshDataList="getDataList 在人人fast这里的 组件标签这里 @refreshDataList="getDataList” 在子组件汇总可以用this.$emit("refreshDataList"); 来调用父组件的getDataList方法

    },
    deviceDelete(id,equipmentType)
    {
      let role =myUtils.getRole(this.$route.query.subjectPkId,this.$store.getters.roles,this.$store.getters.isSystemAdministrator);
      if(role !== 4 && role !== 5 && role !== 6 && role !== 7)
      {
        this.$message.error("您不是分中心管理员");
        return;
      }
      this.$confirm(
        `确定对[设备名为：${equipmentType}]进行删除操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(()=>{
        device.deleteEquipment(id).then(( response ) => {
          console.log(response)
          if (response.code  === '000000') {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 700,
              onClose: () => {
                this.getDataList()
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
        })
    }
  },

}
</script>
<style>


</style>
